package com.forezp.configclient.rabbitConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpConnectException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.stereotype.Component;

/**
 * @version V1.0  </br>
 * @ClassName ReissueMessageConfirmCallback </br>
 * @Description TODO  </br>
 * @Author tonywang</br>
 * @DATE 11/22/2018 3:28 PM</br>
 */
@Component

public class ReissueMessageConfirmCallback implements RabbitTemplate.ConfirmCallback {

    private static final Logger logger = LoggerFactory.getLogger(ReissueMessageConfirmCallback.class);

    private RabbitTemplate rabbitTemplate;

    public ReissueMessageConfirmCallback(RabbitTemplate rabbitTemplate){

        this.rabbitTemplate = rabbitTemplate;

    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause){
        if (correlationData instanceof MessageCorrelationData) {
            MessageCorrelationData messageCorrelationData = (MessageCorrelationData) correlationData;
            String exchange = messageCorrelationData.getExchange();
            String routingKey = messageCorrelationData.getRoutingKey();
            Message message = messageCorrelationData.getMessage();
            if (!ack) {
                try {
                    rabbitTemplate.send(exchange, routingKey, message, messageCorrelationData);
                }catch (AmqpConnectException e) {
                    SendFailedMessageHolder.add(messageCorrelationData);
                }
            }
        }
    }

}